package com.atguigu.pamentdemo.controller;

import com.csvreader.CsvWriter;
import com.google.gson.Gson;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.util.HashMap;

@RestController
@RequestMapping(value="/java",produces = "application/json; charset=utf-8")
public class ConeOnController {

    @GetMapping("comeOn")
    public String aa(HttpServletRequest request) throws UnsupportedEncodingException {
        Gson gson = new Gson();
        HashMap<String, Object> map = new HashMap<>();

        map.put("多喝热水","休息眼睛");
        map.put("认真学习","掌握知识");
        map.put("小子敏","加油!!");
        return gson.toJson(map);
    }


    @GetMapping("/csv")
    public ResponseEntity<byte[]> csv(HttpServletResponse response) throws Exception {
        CSVFormat csvFormat = CSVFormat.DEFAULT.withHeader("1", "2", "3");
        File tempFile = File.createTempFile("vehicle", ".csv");
        FileOutputStream fileOutputStream = new FileOutputStream(tempFile);
        CSVPrinter csvPrinter = new CSVPrinter(new OutputStreamWriter(fileOutputStream), csvFormat);
        for (int i = 0; i < 10; i++) {
            csvPrinter.printRecord("q", "w", "e");
        }
        csvPrinter.flush();
        csvPrinter.close();
        fileOutputStream.close();
        FileInputStream fis = new FileInputStream(tempFile);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] b = new byte[1024];
        int n;
        while ((n = fis.read(b)) != -1)
        {
            bos.write(b, 0, n);
        }
        fis.close();
        bos.close();
        HttpHeaders httpHeaders = new HttpHeaders();
        String fileName = new String("测试.csv".getBytes("UTF-8"), "iso-8859-1");
        httpHeaders.setContentDispositionFormData("attachment", fileName);
        httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        ResponseEntity<byte[]> filebyte = new ResponseEntity<byte[]>(bos.toByteArray(), httpHeaders, HttpStatus.CREATED);
//        response.setContentType("text/csv");
//        response.setHeader("Content-Disposition","attachment; filename=" + URLEncoder.encode(fileName+".csv", "UTF-8"));
//        response.setCharacterEncoding("UTF-8");
        return filebyte;
    }




    @GetMapping("/csvv")
    public void csvv() throws Exception {

        String filePath = "aaa";

        try {
            // 创建CSV写对象
            com.csvreader.CsvWriter csvWriter = new CsvWriter(filePath,',', Charset.forName("GBK"));
            //CsvWriter csvWriter = new CsvWriter(filePath);

            // 写表头
            String[] headers = {"编号","姓名","年龄"};
            String[] content = {"12365","张山","34"};
            csvWriter.writeRecord(headers);
            csvWriter.writeRecord(content);
            csvWriter.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
